package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.common.entity.User;

import java.util.List;

@Mapper
public interface UserMapper {

    // 插入新用户
    @Insert("INSERT INTO user (tenant_id, department_id, username, password, full_name, email, phone_number, status, created_at, updated_at) " +
            "VALUES (#{tenantId}, #{departmentId}, #{username}, #{password}, #{fullName}, #{email}, #{phoneNumber}, #{status}, #{createdAt}, #{updatedAt})")
    int insertUser(User user);

    // 插入新用户包含openId
    @Insert("INSERT INTO user (tenant_id, department_id, username, password, full_name, email, phone_number, status, open_id, created_at, updated_at) " +
            "VALUES (#{tenantId}, #{departmentId}, #{username}, #{password}, #{fullName}, #{email}, #{phoneNumber}, #{status}, #{openId}, #{createdAt}, #{updatedAt})")
    int insertUserWithOpenId(User user);

    // 修改
    @Update("UPDATE user SET department_id = #{departmentId}, username = #{username}, password = #{password}, full_name = #{fullName}, email = #{email}, phone_number = #{phoneNumber}, status = #{status}, updated_at = #{updatedAt} WHERE user_id = #{userId}")
    int updateUser(User user);

    // 根据ID查询用户
    @Select("SELECT * FROM user WHERE user_id = #{userId} AND deleted_at IS NULL")
    User getUserById(@Param("userId") Long userId);

    // 查询某租户下的所有用户
    @Select("SELECT * FROM user WHERE tenant_id = #{tenantId} AND deleted_at IS NULL")
    List<User> getUsersByTenantId(@Param("tenantId") Long tenantId);

    // 逻辑删除用户
    @Update("UPDATE user SET deleted_at = NOW() WHERE user_id = #{userId}")
    int deleteUser(@Param("userId") Long userId);

    // 根据用户名密码查询用户
    @Select("SELECT * FROM user WHERE (username = #{username} AND password = #{password}) OR open_id = #{openId} AND deleted_at IS NULL")
    User selectUserByUsername(User user);

}